package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.ScenicSpotIntroduction;
import cn.wolfcode.wolf2w.query.ScenicSpotIntroductionQuery;
import cn.wolfcode.wolf2w.repository.ScenicSpotIntroductionRepository;
import cn.wolfcode.wolf2w.service.IScenicSpotIntroductionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户服务接口实现
 */
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class ScenicSpotIntroductionServiceImpl implements IScenicSpotIntroductionService {

    @Autowired
    private ScenicSpotIntroductionRepository repsitory;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(ScenicSpotIntroduction scenicSpotIntroduction) {
        repsitory.save(scenicSpotIntroduction);
    }

    @Override
    public void update(ScenicSpotIntroduction scenicSpotIntroduction) {
        repsitory.save(scenicSpotIntroduction);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public ScenicSpotIntroduction get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<ScenicSpotIntroduction> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<ScenicSpotIntroduction> query(ScenicSpotIntroductionQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, query, qo, ScenicSpotIntroduction.class, pageable);
    }

    @Override
    public void saveOrUpdate(ScenicSpotIntroduction scenicSpotIntroduction) {
        if (StringUtils.hasLength(scenicSpotIntroduction.getId())) {
            //update
            this.update(scenicSpotIntroduction);
        } else {
            //save
            this.save(scenicSpotIntroduction);
        }
    }

    @Override
    public ScenicSpotIntroduction findByScenicId(String scenicId) {
        ScenicSpotIntroduction scenicSpotIntroduction = repsitory.findByScenicId(scenicId);
        return scenicSpotIntroduction;
    }
}
